package com.priox.sigar.application.job.mappers;

import com.priox.sigar.client.dto.BalanceComprobacionDTO;
import com.priox.sigar.client.dto.OperacionCuentaDTO;
import com.priox.sigar.enums.TipoCuenta;
import net.sf.jasperreports.engine.util.BigDecimalUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.Null;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * User: Matrix
 * Date: 10/02/13
 */

public class BalanzaFieldSetMapper implements FieldSetMapper<BalanceComprobacionDTO> {


    private static final Logger ERRORLOG = LoggerFactory.getLogger("errorLog");
    private static final Logger LOG = LoggerFactory.getLogger(BalanzaFieldSetMapper.class);

    private Long checkPointId;
    private Long usuarioId;
    private Date fechaCorte;


    /*
     * (non-Javadoc)
     *
     * @see
     * org.springframework.batch.item.file.mapping.FieldSetMapper#mapFieldSet
     * (org.springframework.batch.item.file.transform.FieldSet)
     */
    public static void main(String[] args) {
        String patternNumeros = "[+-]*[0-9,0-9]*.[0-9]*";
        System.out.println("11".matches(patternNumeros));
        System.out.println("11.00".matches(patternNumeros));
        System.out.println("AAAgg8787".matches(patternNumeros));
        System.out.println("15,545,4".matches(patternNumeros));
        System.out.println("15,545,4.22".matches(patternNumeros));
        System.out.println("4.22".matches(patternNumeros));
        System.out.println("-4.22".matches(patternNumeros));
    }
    public BalanceComprobacionDTO mapFieldSet(FieldSet fieldSet) throws BindException {
        BalanceComprobacionDTO result = new BalanceComprobacionDTO();
        String patternFecha = "^\\d{2}.\\d{2}.\\d{4}$";
        String patternNumeros = "[+-]*[0-9,0-9]*.[0-9]*";

        try {

            if (StringUtils.isNotBlank(fieldSet.readString(1))) {
               result.setCuentaContableId(fieldSet.readString(1));
            }

            if (StringUtils.isNotBlank(fieldSet.readString(2))) {
               result.setCuentaContableDescripcion(fieldSet.readString(2));
            }

            if (StringUtils.isNotBlank(fieldSet.readString(9)) && !fieldSet.readString(9).equals("0")){
                if(fieldSet.readString(9).matches(patternNumeros)){
                    result.setSaldoInicial(new BigDecimal(fieldSet.readString(9).replace(",","")));
                }
            }else{
                if(fieldSet.readString(10).matches(patternNumeros)){
                 BigDecimal big= new BigDecimal(fieldSet.readString(10).replace(",","")).multiply(new BigDecimal(-1)) ;

                  //  result.setSaldoInicial(new BigDecimal(fieldSet.readString(10).replace(",","")));
                    result.setSaldoInicial(big);

                }else{
                    result.setSaldoInicial(BigDecimal.ZERO);
                }
            }
            if(NumberUtils.isNumber(fieldSet.readString(13))){
                result.setSociedadId(fieldSet.readString(13));
            }
            result.setCheckPointId(checkPointId);
        } catch (Exception ex) {
            ERRORLOG.debug("error al leer el balance comparativo ",ex);
        }
        return result;
    }

    public Long getCheckPointId() {
        return checkPointId;
    }

    public void setCheckPointId(Long checkPointId) {
        this.checkPointId = checkPointId;
    }

    public Long getUsuarioId() {
        return usuarioId;
    }

    public void setUsuarioId(Long usuarioId) {
        this.usuarioId = usuarioId;
    }

    public Date getFechaCorte() {
        return fechaCorte;
    }

    public void setFechaCorte(Date fechaCorte) {
        this.fechaCorte = fechaCorte;
    }
}


